<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="zh-cn">
    <head>
        <!-- Bootstrap -->
        <include file="Include/css" />
        <title>账号管理</title>
        <link href="__STATICS__/css/admin_list.css" rel="stylesheet">
        <link href="__STATICS__/css/sweet-alert.css" rel="stylesheet">
        <script type="text/javascript" src="__STATICS__/js/sweet-alert.min.js"></script>

    </head>
    <body>
        <include file="Include/nav_top" />
        <div class="container clearfix" id="left_menu_wraper" >
            <include  file="Include/menu_left" />
            <div id="right-content">
                <div id="right-content-inner">
                    <ul class="breadcrumb">
                        <span>您的位置：</span>
                        <li><a href="{:U('CNEPAdmin/Index/index')}">首页</a> </li>
                        <li class="">系统管理</li>
                        <li class="active">帐号管理</li>
                    </ul>


                    <div id="message_wraper" style="display: none;"  class="alert alert-info  alert-dismissible" role="alert">

                        <!--关闭按钮-->
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                        <!--内容-->

                        <strong>温馨提示：</strong> <span id="message" >{$lastsql}</span>
                    </div>
                    <p>
                        <button id="add_look" class="btn btn-orange" type="button" data-id="2" >
                            添加管理员
                        </button>
                    </p>

                    <div role="tabpanel">
                        <div class="row clearfix margin10 form-horizontal">
                            <form  name="query" method="POST" >
                                <!--                            <div class="col-md-2">
                                                                <select name="goods_cat" class="pull-left ml10 form-control">
                                                                    <option value="0">宝贝类别</option>
                                                                    <option value="1">1</option>
                                                                    <option value="2">2</option>
                                                                </select>
                                                            </div>-->
                                <span class="pull-left ml30 form_bar">查询：</span>
                                <div class="col-md-2">
                                    <input type="text" name="sAccount" value="{$Think.post.sAccount}" placeholder=" 登录帐号" class="pull-left ml10 form-control" />
                                </div>
                                <!--                                <div class="col-md-2">
                                                                    <input type="text" name="sNickname" value="{$Think.post.sNickname}" placeholder=" 昵称" class="pull-left ml10 form-control" />
                                                                </div>
                                                                <div class="col-md-2">
                                                                    <input type="text" name="sEmail" value="{$Think.post.sEmail}" placeholder=" 邮箱" class="pull-left ml10 form-control" />
                                                                </div>
                                                                <div class="col-md-2">
                                                                    <input type="text" name="sPhone" value="{$Think.post.sPhone}" placeholder=" 联系方式" class="pull-left ml10 form-control" />
                                                                </div>-->

                                <button type="submit" class="pull-left btn btn-default ml20">
                                    搜索
                                </button>
                            </form>
                        </div>

                        <table class="table table-striped table_qrcode  table-hover">
                            <!--<caption>标题</caption>-->
                            <thead>
                                <tr class="border">
                                    <th>编号</th>
                                    <th>帐号</th>
                                    <th>昵称</th>
                                    <th>邮箱</th>
                                    <th>联系方式</th>
                                    <th>最近登录</th>
                                    <th>登录IP</th>
                                    <th>添加时间</th>
                                    <!--<th>更新时间</th>-->
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <volist name="list" id="vo">
                                    <tr class="qr_tar">
                                        <td>
                                            {$i+$num}
                                        </td>
                                        <td>
                                            <span class="account_td" >{$vo.account}</span>
                                        </td>
                                        <td>{$vo.nickname}</td>
                                        <td>{$vo.email}</td>
                                        <td>{$vo.phone}</td>
                                        <td>{$vo.lastLogin|date="Y/m/d H:i:s",###} </td>
                                        <td>{$vo.lastIp} </td>
                                        <td>{$vo.addTime|date="Y/m/d",###} </td>
                                        <!--<td>{$vo.updateTime|date="Y/m/d H:i:s",###} </td>-->
                                        <td data-id="{$vo.adminID}" data-role="{$vo.roleID}">
                                            <a href="javascript:void(0)" class="edit_look reset_pass" data-id="0"  >重置密码</a>
                                            /
                                            <a href="javascript:void(0)" class="edit_look reset_role" data-id="1"  >修改角色</a>
                                            /
                                            <a href="javascript:void(0)" class="tar_delete" >删除</a> </td>
                                    </tr>
                                </volist>
                            </tbody>
                        </table>

                        <div class="clearfix s_page pull-right">
                            {$page}
                        </div>

                    </div>

                </div>

            </div>
        </div>

        <!-- Modal -->
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel"> </h4>
                    </div>
                    <div class="modal-body">
                        
                            <div class="row clearfix">
                                <div class="col-md-12 margin10 account">
                                    <div class="col-md-2 text_right form_bar">
                                        帐号：
                                    </div>
                                    <div class="col-md-6  form_bar">
                                        <input id="add_account" style="display: none;" type="text" class="form-control"/>
                                        <span id="account"  >
                                            123
                                        </span>
                                    </div>
                                </div>
                                <div class="col-md-12 margin10 pass">
                                    <div class="col-md-2 text_right form_bar">
                                        密码：
                                    </div>
                                    <div class="col-md-6">
                                        <input id="password" type="password" class="form-control"/>
                                    </div>
                                    <div class="col-md-1 text_warning text_left form_bar">
                                        *
                                    </div>
                                </div>
                                <div class="col-md-12 margin10 re_pass">
                                    <div class="col-md-2 text_right form_bar">
                                        确认密码：
                                    </div>
                                    <div class="col-md-6">
                                        <input id="rePassword" type="password" class="form-control"/>
                                    </div>
                                    <div class="col-md-1 text_warning text_left form_bar">
                                        *
                                    </div>
                                </div>
                                <div class="col-md-12 margin10 role">
                                    <div class="col-md-2 text_right form_bar">
                                        角色：
                                    </div>
                                    <div class="col-md-6">
                                        <select id="roleID" class="form-control">
                                            <option value="0" id="role_0">-请选择角色-</option>
                                            <volist name="roleList" id="vor">
                                                <option value="{$vor.roleID}" id="role_{$vor.roleID}" >{$vor.roleName}</option>
                                            </volist>
                                        </select>
                                    </div>
                                    <div class="col-md-1 text_warning text_left form_bar">
                                        *
                                    </div>
                                </div>
                                <div class="col-md-12 margin10 name">
                                    <div class="col-md-2 text_right form_bar">
                                        昵称：
                                    </div>
                                    <div class="col-md-6">
                                        <input id="nickname" type="text" class="form-control"/>
                                    </div>
                                </div>
                                <div class="col-md-12 margin10 phone">
                                    <div class="col-md-2 text_right form_bar">
                                        联系方式：
                                    </div>
                                    <div class="col-md-6">
                                        <input id="phone" type="text" class="form-control"/>
                                    </div>
                                </div>
                                <div class="col-md-12 margin10 mail">
                                    <div class="col-md-2 text_right form_bar">
                                        邮箱：
                                    </div>
                                    <div class="col-md-6">
                                        <input id="email" type="text" class="form-control"/>
                                    </div>
                                </div>
                                <input type="hidden" id="adminID" />

                            </div>

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-primary" id="save" >保存</button>
                    </div>
                </div>
            </div>
        </div>

        <include file="Include/script" />
        <script>
            var isReload = false; // 标识弹出框关闭之后是否刷新页面
            var myDom; // 当前操作的dom对象
            
            /*菜单展开及高亮*/
            $(function () {
                $('.system').addClass('open');
                $('#system1').addClass('active');

                /*弹出框隐藏后*/
                $('#myModal').on('hidden.bs.modal', function (e) {
                     reset_display();
                });
                
                /*重置密码弹窗*/
                $(".reset_pass").on("click", function () {
                    var self = $(this);
                    var dataid = self.attr("data-id");
                    
                    $("#save").attr("data-id",dataid);
                    
                        $("#myModalLabel").html("重置密码");
                        $("#account").html($(this).closest('tr').find('.account_td').html());

                        $('#myModal').modal(); // 跳出弹窗

                    $('.account,.pass,.re_pass').show();
                    
                });
                
                /*重置角色弹窗*/
                $(".reset_role").on("click", function () {
                    var self = $(this);
                    var dataid = self.attr("data-id");
                    
                    $("#save").attr("data-id",dataid);
                    
                    $("#myModalLabel").html("修改角色");
                    $("#account").html(self.closest('tr').find('.account_td').html());

                    $('#myModal').modal(); // 跳出弹窗
                    var roleID = self.closest('td').attr('data-role');
                    
                    $('.account,.role').show();
                    $('#role_'+roleID).attr("selected",true);
                    
                    myDom = self;
                });
                
                /*删除弹窗*/
                $(".tar_delete").on("click", function () {
                    var self = $(this);
                    swal({   title: "您确定吗?",
                                text: "您将删除此账号！",
                                type: "warning",
                                showCancelButton: true,
                                confirmButtonColor: "#DD6B55",
                                confirmButtonText: "是的，删除",
                                cancelButtonText: "放弃",
                                closeOnConfirm: false
                            },
                            function (isConfirm) {
                                if (isConfirm) {
                                    var ajaxData = {};
                                    ajaxData.adminID = self.closest('td').attr('data-id');
                                    ajax_go(ajaxData ,'deleteAdmin');
                                }

                            });
                });
                
                /*添加时候弹窗*/
                $("#add_look").on("click", function() {
                    var self = $(this);
                    var dataid = self.attr("data-id");
                    $("#save").attr("data-id",dataid);
                    
                    $("#account").hide();
                    $("#add_account").show();
                    $('.account,.pass,.re_pass,.role,.name,.phone,.mail').show();
                    $('#myModal').modal(); // 跳出弹窗
                });
                
                /*点击保存*/
                $("#save").on("click", function () {
                   var dataid = parseInt($(this).attr("data-id"));
                   if(dataid === 0) { // 重置密码
                       reset_psw();
                   } else if(dataid === 1) { // 重置角色
                       reset_role();
                   } else if(dataid === 2) { // 添加
                       add_admin();
                   }
                });
                
                /*重置密码*/
                function reset_psw() {
                    if($.trim($('#password').val()) === '')
                    {
                        $('#password').focus();
                        swal('密码不能为空','','warning');
                    }
                    else if($.trim($('#rePassword').val()) === '')
                    {
                        $('#rePassword').focus();
                        swal('请填写确认密码','','warning');
                    }
                    else if($.trim($('#password').val()) != $.trim($('#rePassword').val()) )
                    {
                        $('#rePassword').focus();
                        swal('两次密码不一致','','warning');
                    }
                    else
                    {
                        var ajaxData = {};
                        ajaxData.password = $.trim($('#password').val());
                        ajaxData.rePassword = $.trim($('#rePassword').val());
                        ajaxData.adminID = $(".reset_pass").closest('td').attr('data-id');

                        ajax_go(ajaxData ,"{:U('CNEPAdmin/SysAdmin/resetPsw')}",false);
                    }
                }
                /*重置角色*/
                function reset_role() {
                    if( $.trim($('#roleID').val()) === '0')
                    {
                        $('#roleID').focus();
                        swal('请选择角色','','warning');
                        return false;
                    } else
                    {
                        var ajaxData = {};
                        ajaxData.roleID = $.trim($('#roleID').val());
                        ajaxData.adminID = $(".reset_role").closest('td').attr('data-id');
                        var rt = ajax_go(ajaxData ,"{:U('CNEPAdmin/SysAdmin/updateAdminRole')}",false);
                        
                        myDom.closest('td').attr('data-role',ajaxData.roleID); // 更新列表中的角色
//                            console.log(myDom);
                        
                    }
                }
                /*添加管理员*/
                function add_admin() {
                    if($.trim($('#add_account').val()) === '')
                    {
                        $('#add_account').focus();
                        swal('帐号不能为空','','warning');
                    }
                    else if($.trim($('#password').val()) === '')
                    {
                        $('#password').focus();
                        swal('密码不能为空','','warning');
                    }
                    else if($.trim($('#rePassword').val()) === '')
                    {
                        $('#rePassword').focus();
                        swal('请填写确认密码','','warning');
                    }
                    else if($.trim($('#password').val()) != $.trim($('#rePassword').val()) )
                    {
                        $('#rePassword').focus();
                        swal('两次密码不一致','','warning');
                    }
                    else if( $.trim($('#roleID').val()) === '0')
                    {
                        $('#roleID').focus();
                        swal('请选择角色','','warning');
                        return false;
                    }
                    else
                    {
                        var ajaxData = {};
                        ajaxData.account = $.trim($('#add_account').val());
                        ajaxData.password = $.trim($('#password').val());
                        ajaxData.rePassword = $.trim($('#rePassword').val());
                        ajaxData.roleID = $.trim($('#roleID').val());
                        ajaxData.nickname = $.trim($('#nickname').val());
                        ajaxData.phone = $.trim($('#phone').val());
                        ajaxData.email = $.trim($('#email').val());
                        isReload = true; // 刷新页面
                        ajax_go(ajaxData ,'saveAdmin',true);
                    }
                }

                /*清空弹出框内容*/
                function reset_display()
                {
                    if(isReload) {
                        window.location.reload();
                    }
                    
                    $('.account,.pass,.re_pass,.role,.name,.phone,.mail').hide();
                    $("#account").show();
                    $("#add_account").hide(); // 隐藏帐号输入框
                    
                    $("#account").html("");
                    $("#add_account").val("");
                    $("#password").val("");
                    $("#rePassword").val("");
                    $("#email").val("");
                    $("#phone").val("");
                    $("#nickname").val("");
                    $("#role_0").attr("selected",true);
                    
                }

            });
            


            
        </script>
    </body>
</html>